#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <unistd.h>

namespace ThreadModlue
{
    static uint32_t number = 1;
    //template<typename T>
    class Thread
    {
        //using func_t = std::function<void(T)>;
        using func_t = std::function<void()>;

    private:
        void EnableRuning()
        {
            _isruning = true;
        }
        void EnableDetach()
        {
            std::cout<<"线程被分离"<<std::endl;
            _isdetach = true;
        }
        static void *Routine(void *args)
        {
            //Thread<T> *self = static_cast<Thread<T> *>(args);
            Thread *self = static_cast<Thread *>(args);
            self->EnableRuning(); // 设置运行标志位
            if (self->_isdetach)//检查有没有被分离，如果已经被分离了，不允许重复分离
                self->Detach();
            pthread_setname_np(self->_tid,self->_name.c_str());
            //self->_func(self->_data);
            self->_func();

            return nullptr;
        }
    public:
        // Thread(func_t func,T data)
        //     : _tid(0), _isdetach(false), _isruning(false), res(nullptr), _func(func), _data(data)
        // {
        //     _name = "Thread-" + std::to_string(number++);
        // }
        Thread(func_t func)
            : _tid(0), _isdetach(false), _isruning(false), res(nullptr), _func(func)
        {
            _name = "Thread-" + std::to_string(number++);
        }
        void Detach()
        {
            if (_isdetach)//这里是为了不让重复分离
                return;
            if (_isruning)//如果正在运行就调用phread_detach来分离
                pthread_detach(_tid);
            EnableDetach();
        }

        bool Start()
        {
            if (_isruning) // 不让重复启动
                return false;

            int n = pthread_create(&_tid, nullptr, Routine, this); // 创建线程,传this指针，为了在类内用我们外部定义的方法
            if (n != 0)
            {
                std::cout << "create thread error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << "start success" << std::endl;
                return true;
            }
        }

        bool Stop()
        {
            if (_isruning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cout << "stop thread error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    _isruning = false;

                    std::cout << _name << "stop success" << std::endl;
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            if (_isdetach)
            {
                std::cout<<"线程被分离"<<std::endl;
                return;
            }
            int n = pthread_join(_tid, &res);
            if (n != 0)
            {
                std::cout << "join thread error: " << strerror(n) << std::endl;
                return;
            }
            else
            {
                std::cout << "join success" << std::endl;
            }
        }
        pthread_t Id()
        {
            return _tid;
        }
        ~Thread()
        {
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isruning;
        void *res;
        func_t _func;
    };
}

#endif